import axios from 'axios'
import { Message } from 'element-ui'

axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8';
// 环境的切换
// if (process.env.NODE_ENV === 'development') {
//   axios.defaults.baseURL = ''
// } else if (process.env.NODE_ENV === 'debug') {
//   axios.defaults.baseURL = ''
// } else if (process.env.NODE_ENV === 'production') {
//   axios.defaults.baseURL = ''
// }
// 创建axios实例
const service = axios.create({
  // axios中请求配置有baseURL选项，表示请求URL公共部分
  baseURL: 'http://localhost:8080',
  // 超时
  timeout: 10000
})
// request拦截器
service.interceptors.request.use(config => {
  // eslint-disable-next-line no-unused-vars
    let url = config.url.replace(config.baseURL, '');
  // eslint-disable-next-line no-unused-vars
    let code = config.code;
    return config
}, error => {
  // eslint-disable-next-line no-console
    console.log(error)
    Promise.reject(error)
})

// 响应拦截器

service.interceptors.response.use(function(response){
        return response.data;

  }, (error)=> {
        Message({
            message: error.message,
            type: 'error',
            duration: 5 * 1000
        })
        return Promise.reject(error)
    }
)

export default service
